package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.entity.SpuImage;
import com.atguigu.gmall.model.product.entity.SpuSaleAttr;
import com.atguigu.gmall.model.product.entity.SpuSaleAttrValue;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.service.SpuSaleAttrValueService;
import com.atguigu.gmall.model.product.vo.SaveSpuInfoDetailVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.model.product.entity.SpuInfo;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.mapper.SpuInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author lyd
* @description 针对表【spu_info(商品表)】的数据库操作Service实现
* @createDate 2025-03-10 20:57:08
*/
@Service
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo>
    implements SpuInfoService{


    @Autowired
    SpuImageService spuImageService;
    @Autowired
    SpuSaleAttrService spuSaleAttrService;
    @Autowired
    SpuSaleAttrValueService spuSaleAttrValueService;
    @Transactional
    @Override
    public void saveSpuInfo(SaveSpuInfoDetailVo vo) {
        //1、保存spu_info
        SpuInfo spuInfo = new SpuInfo();
        BeanUtils.copyProperties(vo,spuInfo);
        this.save(spuInfo);
        Long spuId = spuInfo.getId();

        //2、保存spu_image
        List<SpuImage> spuImages = vo.getSpuImageList().stream()
                .map(item -> {
                    SpuImage spuImage = new SpuImage();
                    spuImage.setSpuId(spuId);
                    spuImage.setImgName(item.getImgName());
                    spuImage.setImgUrl(item.getImgUrl());
                    return spuImage;
                }).collect(Collectors.toList());
        spuImageService.saveBatch(spuImages);
        //3、保存spu_sale_attr
        List<SpuSaleAttr> spuSaleAttrs = vo.getSpuSaleAttrList().stream()
                .map(item -> {
                    SpuSaleAttr spuSaleAttr = new SpuSaleAttr();
                    spuSaleAttr.setSpuId(spuId);
                    spuSaleAttr.setBaseSaleAttrId(item.getBaseSaleAttrId());
                    spuSaleAttr.setSaleAttrName(item.getSaleAttrName());
                    return spuSaleAttr;
                }).collect(Collectors.toList());
        spuSaleAttrService.saveBatch(spuSaleAttrs);
        //4、保存spu_sale_attr_value
        List<SpuSaleAttrValue> spuSaleAttrValues = vo.getSpuSaleAttrList().stream()
                .flatMap(item ->
                    item.getSpuSaleAttrValueList().stream()
                            .map(i->{
                                i.setSaleAttrName(item.getSaleAttrName());
                                return i;
                            })
                )
                .map(item -> {
                    SpuSaleAttrValue spuSaleAttrValue = new SpuSaleAttrValue();
                    spuSaleAttrValue.setSpuId(spuId);
                    spuSaleAttrValue.setBaseSaleAttrId(item.getBaseSaleAttrId());
                    spuSaleAttrValue.setSaleAttrValueName(item.getSaleAttrValueName());
                    spuSaleAttrValue.setSaleAttrName(item.getSaleAttrName());
                    return spuSaleAttrValue;
                }).collect(Collectors.toList());
        spuSaleAttrValueService.saveBatch(spuSaleAttrValues);
    }
}




